package kmitl.comsci.bustacker;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

import kmitl.comsci.bustacker.R;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HomeActivity extends FragmentActivity {
	
	private String latt,lonn;
	private double double_lat,double_lon;
	private GoogleMap mMap;
	private LatLng coordinates;
	
	private static LocationManager myLocationManager;
	private static LocationListener myLocationListener;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_map);
        
        myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        myLocationListener = new MyLocationListener();
        
        coordinates = new LatLng(double_lat, double_lon);
        Log.d("double_lat", ""+double_lat);
        Log.d("double_lon", ""+double_lon);
        
        mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.home_map)).getMap();
        //mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
        mMap.animateCamera(CameraUpdateFactory.newLatLng(coordinates));
		
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        
		Button buttonSearch = (Button)findViewById(R.id.home_buttonSearch);
		buttonSearch.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(HomeActivity.this, SearchActivity.class);
				startActivity(intent);
				//finish();
			}
		});
		
		Button buttonSuggestion = (Button)findViewById(R.id.home_buttonSuggestion);
		buttonSuggestion.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(HomeActivity.this, SuggestionActivity.class);
				startActivity(intent);
				//finish();
			}
		});
		
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.home_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
			
		case R.id.LinkAbout:
			intent = new Intent(HomeActivity.this, AboutActivity.class);
			startActivity(intent);
			return true;
			
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	
	private class MyLocationListener implements LocationListener{
        public void onLocationChanged(Location argLocation) {
            latt = String.valueOf(argLocation.getLatitude());
            lonn = String.valueOf(argLocation.getLongitude());
            Log.d("latt", latt);
            Log.d("lonn", lonn);
            double_lat = Double.parseDouble(latt);
            double_lon = Double.parseDouble(lonn);
        }
        public void onProviderDisabled(String provider) {   }
        public void onProviderEnabled(String provider) {    }
        public void onStatusChanged(String provider,int status, Bundle extras) {    }
    };
    
    

}
